/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2018
 */

package com.bm.console.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bm.console.business.WebNewsTypeService;
import com.bm.console.pojo.Tree;
import com.bm.console.pojo.WebNewsType;
import com.sys.console.pojo.Sys_Authority;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;
import com.sys.object.common.page.PageBean;

@Controller
public class WebNewsTypeAction {

    @Resource
    WebNewsTypeService webNewsTypeService;

    @RequestMapping(value = "/webNewsTypeList.do")
    public String webNewsTypeList(PageBean pageBean, Model model, WebNewsType webNewsType) {
        try {
            pageBean = webNewsTypeService.queryPageList(webNewsType, pageBean);
            model.addAttribute("pageBean", pageBean);
            model.addAttribute("startRow", pageBean.getStartRow());
            model.addAttribute("entity", webNewsType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/web/webnewstype/list";
    }

    /**
     * 新增动态 Get
     */
    @RequestMapping(value = "/addWebNewsType.do", method = RequestMethod.GET)
    public String addWebNewsType(@ModelAttribute("webNewsType") WebNewsType webNewsType, Model model, String id) {
        Integer prow = 0;
        try {
            if (StringUtils.hasText(id)) {
                WebNewsType record = webNewsTypeService.queryByPrimaryKey(Integer.parseInt(id));
                model.addAttribute("entity", record);
            }

            WebNewsType recod = new WebNewsType();
            recod.setPid(0L);
            List<Tree> cate_list = webNewsTypeService.selectAll(recod);
            model.addAttribute("cate_list", cate_list);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        model.addAttribute("prow", prow);
        return "/web/webnewstype/add";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebNewsType.do", method = RequestMethod.POST)
    public String saveWebNewsType(@Validated @ModelAttribute("webNewsType") WebNewsType webNewsType,
                                  BindingResult result, Model model, Integer prow,
                                  HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webNewsTypeService.saveOrUpdate(webNewsType);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {

            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);

            }
            e.printStackTrace();
            model.addAttribute("entity", webNewsType);
            model.addAttribute("ret", ret);
            return "/web/webnewstype/add";
        }
        return "redirect:/webNewsTypeList.do?status=";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebNewsTypeJson.do", method = RequestMethod.POST)
    @ResponseBody
    public ObjResultException<String> saveWebNewsTypeJson(@RequestBody WebNewsType webNewsType, Model model,
                                                          HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webNewsTypeService.saveOrUpdate(webNewsType);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }

    /**
     * 删除APP
     */
    @RequestMapping("/deleteWebNewsType.do")
    @ResponseBody
    public ObjResultException<String> deleteWebNewsType(Integer id) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webNewsTypeService.delete(id);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 修改状态APP
     */
    @RequestMapping("/updateStatusWebNewsType.do")
    @ResponseBody
    public ObjResultException<String> updateStatusWebNewsType(WebNewsType webNewsType) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webNewsTypeService.updateStatus(webNewsType);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }


    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/webNewesType/tree.do", method = RequestMethod.POST)
    @ResponseBody
    public Object tree(Model model) {
        ObjResultException<Tree> ret = new ObjResultException<Tree>();
        ObjResultJSON<List<Tree>> retjson = new ObjResultJSON<List<Tree>>();
        try {
            WebNewsType webNewsType = new WebNewsType();
            webNewsType.setStatus(true);
            List<Tree> list = webNewsTypeService.selectAll(webNewsType);
            if (list.size() > 0) {
                return JSON.toJSON(list);
                //retjson.setResultCode(ObjResultCode.OK);
            } else {
                retjson.setResultCode(ObjResultCode.ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }

            model.addAttribute("ret", ret);
        }
        return retjson;
    }
}